package com.orhon.smartcampus.modules.teacher.fetcher;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.teacher.dao.QualificationsOptionsDao;
import com.orhon.smartcampus.modules.teacher.entity.QualificationsOptions;
import com.orhon.smartcampus.modules.teacher.gqlModel.GQLQualificationsOption;
import com.orhon.smartcampus.modules.teacher.mm.QualificationsOptionMapper;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class QualificationsOptionssFetcher implements DataFetcher {
	
	@Autowired
	QualificationsOptionsDao qualificationsOptionsDao;

	@Override
	public Object get(DataFetchingEnvironment env) throws Exception {

		GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(env);
		String hql = "from QualificationsOptions where deleted_at = null ";
		if(env.getArgument("pid")!=null) {
			int id = Integer.parseInt(env.getArgument("pid").toString());
			hql += " and pid = " + id;
		}
		List<QualificationsOptions> list = qualificationsOptionsDao.find(hql,null,page.getPage(),page.getLimit());

		List<GQLQualificationsOption> mapEntityList = QualificationsOptionMapper.MAPPER.mapEntityList(list);

		Long total = 0L;
		if (env.getSelectionSet().contains("total")){
			total = qualificationsOptionsDao.getTotalCount(hql);
		}
		return Result.ok().put("data", mapEntityList).put("total", total);
	}
}
